﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HZY.Admin.Controllers.Sys
{
    using HZY.Logic.Sys;
    using HZY.Models.Sys;
    using HZY.DTO;
    using HZY.DTO.Sys;
    using HZY.Toolkit;

    public class UserController : ApiBaseController
    {
        protected Sys_UserLogic logic { get; }
        protected Sys_MenuLogic sys_MenuLogic { get; set; }
        protected AccountLogic accountLogic { get; set; }

        public UserController(Sys_UserLogic _logic, Sys_MenuLogic _sys_MenuLogic, AccountLogic _accountLogic)
        {
            this.logic = _logic;
            this.sys_MenuLogic = _sys_MenuLogic;
            this.accountLogic = _accountLogic;
        }

        #region 基础 CURD

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        [HttpPost("FindList/{Page}/{Rows}"), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> FindListAsync(int Page, int Rows, [FromBody] Sys_User Search)
        {
            var tableVM = await this.logic.FindListAsync(Page, Rows, Search);
            return this.StatusCodeOK(tableVM);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("Save"), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> SaveAsync([FromBody]Sys_UserDto Model)
        {
            await this.logic.SaveAsync(Model);
            return this.StatusCodeOK();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpPost("Delete"), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> DeleteAsync(List<Guid> Ids)
        {
            await this.logic.DeleteAsync(Ids);
            return this.StatusCodeOK();
        }

        /// <summary>
        /// 根据Id 加载表单数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost("LoadForm/{Id}"), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> LoadFormAsync(Guid Id)
        {
            await this.logic.LoadFormAsync(Id);
            return this.StatusCodeOK();
        }

        #endregion

        #region 其他


        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetPowerState/{MenuId}"), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> GetPowerState(Guid MenuId)
        {
            if (MenuId == Guid.Empty) throw new MessageBox("参数MenuId不能为空!");
            return this.StatusCodeOK(new { powerState = await this.sys_MenuLogic.GetPowerStateByMenuId(MenuId) });
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        [HttpPost(nameof(UpdatePassword)), Core.HZYApiAuthorizationCheckFilter]
        public async Task<IActionResult> UpdatePassword([FromBody]UpdatePasswordDto Model)
        {
            await this.accountLogic.ChangePwd(Model.OldPwd, Model.NewPwd);
            return this.StatusCodeOK();
        }


        #endregion

    }
}